package cn.yunbamboos.exception;


import cn.hutool.core.util.StrUtil;

import java.text.MessageFormat;

/**
 * 自定义异常类
 */
public class AppException extends RuntimeException {

    /**
     * String aa = "欢迎您 {0} ，祝您生活愉快！ {1}";
     * MessageFormat.format(aa, "111","哈哈哈");
     */
    public static AppException c(String error, Object... arguments) {
        String msg = StrUtil.format(error, arguments);
        return new AppException(500, msg);
    }

    public static AppException c(int code, String error, Object... arguments) {
        String msg = StrUtil.format(error, arguments);
        return c(code, msg);
    }

    public static AppException c(String msg) {
        return c(500, msg);
    }

    public static AppException c(int code, String msg) {
        return new AppException(code, msg);
    }

    /**
     * 程序异常编码
     */
    private final int code;
    /**
     * 程序异常内容
     */
    private final String msg;

    /**
     * 程序异常{@code msg}
     *
     * @param code 错误代码
     * @param msg  程序异常内容
     */
    AppException(int code, String msg) {
        super(MessageFormat.format("code:{0} msg:{1}", code, msg));
        this.code = code;
        this.msg = msg;
    }

    /**
     * 程序异常编码
     *
     * @return 程序异常编码
     */
    public int getCode() {
        return code;
    }

    /**
     * 程序异常内容
     *
     * @return 程序异常内容
     */
    public String getMsg() {
        return msg;
    }
}

